(function(lazyload)
{	
	if("function"==typeof define && define.amd && define){
		define(['jquery'],lazyload);
	}
}(function($){
	$.fn.extend({
		lazyload:function(option){

			var elements = $(this);
			var _def = {
				threshold       : 0,
	            event           : "scroll",
	            effect          : "show",
	            effect_speed	: 200,
	            css_loading		: "loading",
	            container       : window,
	            data_attribute  : "original",
	            appear			: null,
	            load			: null,
	            placeholder     : ""
        	};
			$.extend(_def,option || {});
			
        	function update(){
        		elements.each(function(){
        			if($.in_fold(this,_def)){
        				$(this).trigger("appear");
        				if ($(this).is("img")) {
        					$(this).attr("src",_def.placeholder);
        				}else{
        					$(this).addClass(_def.css_loading);
        				}
        			}
        		});
        	}

        	$(elements).each(function(){
        		var self = this;
        		var $self = $(self);
        		self.loaded = false;
        		$self.one("appear",function(){
        			var img = $("<img>").one("load",function(){
        				var original = $self.attr("data-" + _def.data_attribute);
                        $self.hide();
                        if ($self.is("img")) {
                            $self.attr("src", original);
                        } else {
                            $self.css("background-image", "url('" + original + "')");
                        }
                        $self[_def.effect](_def.effect_speed);
                        self.loaded = true;
                        var temp = $.grep(elements, function(element) {
                            return !element.loaded;
                        });
                        elements = $(temp);
                        if (_def.load) {
                            settings.load.call(self, _def);
                        }
        				
        			}).attr("src",$self.attr("data-" + _def.data_attribute) );
        			if(_def.appear){
        				_def.appear.call(self);
        			}
        		});
        	});
        	
        	var $container = (_def.container === undefined ||
        			_def.container === window) ? window : $(_def.container);
        	
        	$($container).on(_def.event,function(){
        		update();
        	});
        	
        	$($container).on("resize",function(){
        		update();
        	});

        	//默认运行一次
        	update();
		},	

		/**
		 * 滚动触发
		 *
		 *option : {
		 *		container:window,
		 *		css_loading:"loading",
		 *		event:"scrollIn",
		 *		callback:
		 *		enablevalidate:{}
		 * }
		 */
		scrollToTrigger:function(option){
			if(!this)return;
			var $elements = $(this);
			var settings = {
					container: window,
					event:"scrollIn",
					css_loading:"loading",
					enablevalidate:{validate:function(){return true;}}
			}
			$.extend(settings ,option || {});
			
			var $container = (settings.container === undefined ||
                     settings.container === window) ? window : $(settings.container);
			
			$($container).on("scroll",function(){
				update();
			});
			$($container).on("resize",function(){
				update();
			});
			function update(){
				$elements.each(function(){
					var $self = this;
					$(this).addClass(settings.css_loading);
					if(!$(this).attr("trigger"))
					{
						if($.in_fold($self,{
							container:settings.container,
							threshold:0}))
						{
							if(settings.enablevalidate.validate.call($self)){
								$($self).attr("trigger","1");
								if(settings.callback){
									settings.callback.call($self);
								}else if(settings.event){
									$($self).trigger(settings.event);
								}
								$($self).removeClass(settings.css_loading);
							}
						}
					}
				});
				$elements = $elements.filter(function(){
					return !$(this).attr("trigger");
				});
			}
			update();
		}
	});	
	
	/**
	 * element:
	 * settings:{
	 * 	threshold:
	 *  container:
	 * }
	 */
	$.below_fold = function(element,settings){
		var fold;

        if (settings.container === undefined || settings.container === window) {
            fold = (window.innerHeight ? window.innerHeight : $window.height()) + $window.scrollTop();
        } else {
            fold = $(settings.container).offset().top + $(settings.container).height();
        }

        return fold <= $(element).offset().top - settings.threshold;
	}
	
	/**
	 * 在容器的范围内
	 * element:
	 * settings:{
	 * 	threshold:
	 *  container:
	 * }
	 */
	$.in_fold = function(element,settings){
		var fold;
		if (settings.container === undefined || settings.container === window) {
	        fold = (window.innerHeight ? window.innerHeight : $window.height()) + $window.scrollTop();
	    } else {
	        fold = $(settings.container).offset().top + $(settings.container).height();
	    }
		
		return fold >= $(element).offset().top + settings.threshold;
	}
}));
